package zencstudio.sgrealtor.adapter;

import java.util.ArrayList;
import java.util.List;

import zencstudio.sgrealtor.model.Property;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import zencstudio.sgrealtor.R;
import com.squareup.picasso.Picasso;

public class PropertyGirdViewAdapter extends BaseAdapter{
	private List<Property>lstProperties = new ArrayList<Property>();
	private Context context;
	public PropertyGirdViewAdapter(Context context) {
		this.context = context;
	}
	@Override
	public int getCount() {
		return lstProperties.size();
	}

	@Override
	public Object getItem(int arg0) {
		return lstProperties.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int arg0, View view, ViewGroup arg2) {
		LayoutInflater inflater = 
	              (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		ViewHolder holder = null;
		if(view==null){
			view = inflater.inflate(R.layout.property_item, arg2,false);
			holder = new ViewHolder();
			ImageView imageView = (ImageView) view.findViewById(R.id.imgProperty);			
			holder.imageView = imageView;
			holder.txtName = (TextView) view.findViewById(R.id.txtName);
			holder.txtPrice = (TextView) view.findViewById(R.id.txtPrice);
			holder.txtExcerpt = (TextView) view.findViewById(R.id.txtExcerpt);
			holder.txtBeds = (TextView) view.findViewById(R.id.txtBeds);
			holder.txtBaths = (TextView) view.findViewById(R.id.txtBaths);
			view.setTag(holder);
		}else
		{
			holder = (ViewHolder) view.getTag();
		}
		Property p = lstProperties.get(arg0);
		holder.txtName.setText(p.getName());
		holder.txtBeds.setText(p.getBedrooms());
		holder.txtBaths.setText(p.getBathrooms());
		holder.txtPrice.setText(p.getPriceFormat());
		holder.txtExcerpt.setText(p.getExcerpt());
		try {
			String url = p.getImgThumbnailUrl();	
			Picasso.with(context).load(url).fit().centerInside().error(R.drawable.noimage).placeholder(R.drawable.noimage).into(holder.imageView);
			
		} catch (Exception e) {
			holder.imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.noimage));		
		}
		return view;
	}
	public List<Property> getLstProperties() {
		return lstProperties;
	}
	public void setLstProperties(List<Property> lstProperties) {
		this.lstProperties = lstProperties;
	}
	public void addProperties(List<Property>p){
		this.lstProperties.addAll(p);
	}
	class ViewHolder {
		ImageView imageView;
		TextView txtName;
		TextView txtPrice;
		TextView txtExcerpt;
		TextView txtBeds;
		TextView txtBaths;
	}

}
